'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Public Enum MetaRequestType As Integer
    OfflineMessageRequest = &H3C
    MetaInformationRequest = &H7D0
    DeleteOfflineMessage = &H3E
End Enum

Public Enum MetaRequestSubType As Integer
    SetUserBasicInfoRequest = &H3EA
    SetUserWorkInfoRequest = &H3F3
    SetUserMoreInfoRequest = &H3FD
    SetUserNotesInfoRequest = &H406
    SetUserExtendedEmailInfoRequest = &H40B
    SetUserInterestsInfoRequest = &H410
    SetUserAffilationsInfoRequest = &H41A
    SetUserPermissionsInfoRequest = &H424
    ChangeUserPasswordRequest = &H42E
    SetUserHomepageCategoryInfoRequest = &H442
    RequestFullUserInfo = &H4B2
    RequestShortUserInfo = &H4BA
    UnregisterUserRequest = &H4C4
    RequestFullUserInfo2 = &H4D0
    SearchByDetailsRequestPlain = &H515
    SearchByUinRequestPlain = &H51F
    SearchByEmailRequestPlain = &H529
    WhitePagesSearchRequestPlainSimple = &H533
    SearchByDetailsRequestPlainWildcard = &H53D
    SearchByEmailRequestPlainWildcard = &H547
    WhitePagesSearchRequestPlainWildcard = &H551
    SearchByUinRequestTlv = &H569
    WhitepagesSearchRequestTlv = &H55F
    SearchByEmailRequestTlv = &H573
    RandomChatUserSearchRequest = &H74E
    RequestServerVariableViaXml = &H898
    SendRegistrationStatsReport = &HAA5
    SendShortcutBarStatsReport = &HAAF
    SaveInfoTlvBasedRequest = &HC3A
    ClientSendSMSRequest = &H1482
    ClientSpamReportRequest = &H2008
End Enum

Public MustInherit Class MetaRequest
    Implements ISerializable

    Public Const SizeFixPart As Integer = 10

    Public Sub New(ByVal requestType As MetaRequestType)
        _RequestType = requestType
    End Sub

    Private Shared reqestSequenceNumber As Integer = 0

    Public Shared Function GetNextSequenceNumber() As Integer
        reqestSequenceNumber += 1
        Return reqestSequenceNumber
    End Function

    Public MustOverride Function CalculateDataSize() As Integer Implements ISerializable.CalculateDataSize

    Public Function CalculateTotalSize() As Integer Implements ISerializable.CalculateTotalSize
        Return SizeFixPart + CalculateDataSize()
    End Function

    Private _DataSize As Integer
    Public ReadOnly Property DataSize() As Integer Implements ISerializable.DataSize
        Get
            Return _DataSize
        End Get
    End Property

    Protected Sub SetDataSize(ByVal index As Integer)
        _DataSize = index - SizeFixPart
    End Sub

    Public ReadOnly Property TotalSize() As Integer Implements ISerializable.TotalSize
        Get
            Return DataSize
        End Get
    End Property

    Private _HasData As Boolean
    Public ReadOnly Property HasData() As Boolean Implements ISerializable.HasData
        Get
            Return _HasData
        End Get
    End Property

    Private _ClientUin As Long
    Public Property ClientUin() As Long
        Get
            Return _ClientUin
        End Get
        Set(ByVal value As Long)
            _ClientUin = value
        End Set
    End Property

    Private _RequestType As MetaRequestType
    Public Property RequestType() As MetaRequestType
        Get
            Return _RequestType
        End Get
        Set(ByVal value As MetaRequestType)
            _RequestType = value
        End Set
    End Property

    Private _RequestSequenceNumber As Integer
    Public Property RequestSequenceNumber() As Integer
        Get
            Return _RequestSequenceNumber
        End Get
        Set(ByVal value As Integer)
            _RequestSequenceNumber = value
        End Set
    End Property

    Public Overridable Sub Deserialize(ByVal data As System.Collections.Generic.List(Of Byte)) Implements ISerializable.Deserialize
        'Dim index As Integer

        '_DataSize = ByteConverter.ToUInt16LE(data.GetRange(index, 2)) - (SizeFixPart - 2)
        'index += 2

        '_ClientUin = ByteConverter.ToUInt32LE(data.GetRange(index, 4))
        'index += 4

        '_RequestType = DirectCast(CInt(ByteConverter.ToUInt16LE(data.GetRange(index, 2))), MetaRequestType)
        'index += 2

        '_RequestSequenceNumber = ByteConverter.ToUInt16LE(data.GetRange(index, 2)) - (SizeFixPart - 2)
        'index += 2

        '_HasData = True
        Throw New NotImplementedException
    End Sub

    Public Overridable Function Serialize() As System.Collections.Generic.List(Of Byte) Implements ISerializable.Serialize
        Dim data As List(Of Byte) = New List(Of Byte)

        data.AddRange(ByteConverter.GetBytesLE(CUShort(Me.CalculateDataSize + (SizeFixPart - 2))))
        data.AddRange(ByteConverter.GetBytesLE(CUInt(_ClientUin)))
        data.AddRange(ByteConverter.GetBytesLE(CUShort(_RequestType)))
        data.AddRange(ByteConverter.GetBytesLE(CUShort(_RequestSequenceNumber)))

        Return data
    End Function
End Class